package com.newbie.iSee;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import com.newbie.iSee.MenuPage.Function;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class GalleryPage extends Activity {
    /** Called when the activity is first created. */
	private DateFormat dateFormat = new SimpleDateFormat("M/d");
    private LocalDatabase.Album[] mAlbums;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gallery);

        mAlbums = ShareTools.localDatabase.getAlbums();

        GridView gridview = (GridView) findViewById(R.id.gallery_albums);
        gridview.setAdapter(new ImageAdapter());
        
        gridview.setOnItemClickListener(getActions());
    }
    
    public class ImageAdapter extends BaseAdapter {
        
        public ImageAdapter() {
        }

        public int getCount() {
            return mAlbums.length;
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
			View v;
            if (convertView == null) {  // if it's not recycled, initialize some attributes
				LayoutInflater li = getLayoutInflater();
				v = li.inflate(R.layout.icon, null);
				TextView tv = (TextView) v.findViewById(R.id.icon_text);
				tv.setText(mAlbums[position].name);
				ImageView iv = (ImageView) v.findViewById(R.id.icon_image);
				iv.setImageResource(R.drawable.album);
            } else {
            	v = convertView;
            }

            return v;
        }

        // references to our images

    }
    
	private AdapterView.OnItemClickListener getActions() {
		AdapterView.OnItemClickListener actions = new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView parent, View view,
					int position, long id) {
				Intent change = new Intent(view.getContext(), AlbumPage.class);
				change.putExtra("album_id", mAlbums[position].id);
				if (change != null) {
					startActivityForResult(change, 0);
				}
			}
		};
		return actions;
	}
}